﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Resources;
using System.Reflection;
using System.IO;

namespace Mindfor.Web
{
	/// <summary>
	/// Gives access to mime-types conversion tools.
	/// </summary>
	public static class MimeTypes
	{
		static Dictionary<string, string> s_mime;

		/// <summary>
		/// Loads mime-types.
		/// </summary>
		static MimeTypes()
		{
			// load mime from resources
			s_mime = new Dictionary<string, string>();
			Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Mindfor.Web.MimeTypes.txt");
			if (stream != null)
			{
				using (StreamReader sr = new StreamReader(stream))
				{
					string line;
					while ((line = sr.ReadLine()) != null)
					{
						string[] s = line.Split('\t');
						if (s.Length >= 2)
							s_mime.Add(s[0], s[1]);
					}
				}
			}			
		}

		/// <summary>
		/// Returns mime-type by file extension.
		/// </summary>
		/// <param name="extension">File extension.</param>
		/// <returns>Mime-type.</returns>
		public static string GetByExtension(string extension)
		{
			string ext = extension.ToLower();
			return s_mime.ContainsKey(ext) ? s_mime[ext] : "application/octet-stream";
		}
	}
}
